package com.example.onion.hxchat.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.example.onion.hxchat.activity.ScanUserActivity;
import com.example.databean.Msg;
import com.example.superyi.myteam.R;

import java.util.List;

/**
 * 显示聊天信息的适配器，尚需根据msg类的修改而修改
 */
public class MyMsgAdapter extends RecyclerView.Adapter<MyMsgAdapter.ViewHolder> {

    private List<Msg> mMsgList;

    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLayout;

        LinearLayout rightLayout;

        /**
         * 接收到的消息
         */
        TextView leftMsg;

        /**
         * 发送的消息
         */
        TextView rightMsg;

        /**
         * 对方头像
         */
        ImageView leftImg;

        /**
         * 己方头像
         */
        ImageView rightImg;

        /**
         * 聊天对象用户名（其实就是收到的消息的发送人）
         */
        private TextView chatObjectUserName;
        /**
         * 自己的用户名（其实就是聊天界面自己发送的消息的发送人）
         */
        private TextView chatOwnUserName;

        /**
         * 消息时间
         */
        private TextView msgDate;

        private Context mContext;

        /**
         * 得到子项的部件
         */
        public ViewHolder(View view) {
            super(view);
            leftLayout = view.findViewById(R.id.left_layout);
            rightLayout = view.findViewById(R.id.right_layout);
            leftMsg = view.findViewById(R.id.left_msg);
            rightMsg = view.findViewById(R.id.right_msg);
            leftImg = view.findViewById(R.id.img_left_msg);
            rightImg = view.findViewById(R.id.img_right_msg);
            msgDate = view.findViewById(R.id.msg_time);
        }
    }

    public MyMsgAdapter(List<Msg> msgList) {
        mMsgList = msgList;
    }

    public MyMsgAdapter(List<Msg> mMsgList, Context mContext) {
        this.mMsgList = mMsgList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载子项的布局文件，得到视图，传入ViewHolder,套路写法
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            // 如果是收到的消息，则显示左边的消息布局，将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());//消息
            holder.leftImg.setImageResource(R.drawable.user);//头像
//            holder.chatObjectUserName.setText(msg.getUserName());//左边的用户名
            holder.msgDate.setText(msg.getTime().toString());//消息时间
        } else if (msg.getType() == Msg.TYPE_SENT) {
            // 如果是发出的消息，则显示右边的消息布局，将左边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
            holder.rightImg.setImageResource(R.drawable.user);
//            holder.chatOwnUserName.setText(msg.getUserName());
            holder.msgDate.setText(msg.getTime().toString());//消息时间
        }

        holder.leftImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ScanUserActivity.class);
                intent.putExtra("scanUserId",msg.getUserId());
                intent.putExtra("scanUserName",msg.getUserId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

    /**
     * 得到数据的长度
     *
     * @return 返回数据集合的长度
     */
    public int getDataListSize() {
        return mMsgList.size();
    }
}
